package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.core.common.annotion.Permission;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.core.util.UUIDutil;
import cn.stylefeng.guns.modular.api.HttpClientService;
import cn.stylefeng.guns.modular.system.entity.BankCard;
import cn.stylefeng.guns.modular.system.service.BankCardService;
import cn.stylefeng.guns.modular.system.warpper.BankcardWarpper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 银行卡控制器
 */
@Controller
@RequestMapping("/bankcard")
public class BankCardController extends BaseController {

    private static String PREFIX = "/system/bankcard/";

    @Autowired
    private BankCardService bankCardService;

    @Autowired
    private HttpClientService httpClientService;

    /**
     * 跳转到银行卡页面
     *
     */
    @RequestMapping("")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        Long userId = ShiroKit.getUserNotNull().getId();
        if (ToolUtil.isEmpty(userId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        List<Map<String, Object>> bankcard = httpClientService.list(userId);
        if (bankcard.size() > 0){
            modelAndView.addObject("bankCard",new BankcardWarpper(bankcard).wrap());
            modelAndView.setViewName(PREFIX + "hasbankcard.html");
            return modelAndView;
        }
        modelAndView.setViewName(PREFIX + "bankcard.html");
        return modelAndView;
    }

    /**
     * 跳转到添加银行卡
     *
     * @return
     */
    @RequestMapping("/bankcard_add")
    public String addView() {
        return PREFIX + "bankcard_add.html";
    }

    /**
     * 跳转到修改银行卡
     *
     * @return
     */
    @RequestMapping("/bankcard_edit")
    public String editView(@RequestParam Long cardId) {
        if (ToolUtil.isEmpty(cardId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        BankCard bankCard = this.bankCardService.getById(cardId);
        LogObjectHolder.me().set(bankCard);
        return PREFIX + "bankcard_edit.html";
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResponseData add(@Valid BankCard bankCard, BindingResult result) {
        if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        Long userId = ShiroKit.getUserNotNull().getId();
        if (ToolUtil.isEmpty(userId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        //生成16位银行卡卡号
        bankCard.setCardNumber(Long.valueOf(UUIDutil.randomUUId()));
        httpClientService.bingDingCard(bankCard, userId);
        return SUCCESS_TIP;
    }

    /**
     * 充钱
     * @param money
     * @param result
     * @return
     */
    /*@RequestMapping("/addMoney")
    @ResponseBody
    public ResponseData addMoney(@RequestParam Integer money, BindingResult result) {
        if (ToolUtil.isEmpty(money)) {
            throw new ServiceException(BizExceptionEnum.NOT_MONEY);
        }
        if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        Long userId = ShiroKit.getUserNotNull().getId();
        if (ToolUtil.isEmpty(userId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        httpClientService.addMoney(money, userId);
        return SUCCESS_TIP;
    }*/

    /**
     * 修改银行卡信息
     *
     * @param bankCard
     * @param result
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public ResponseData edit(@Valid BankCard bankCard, BindingResult result) {
        if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        httpClientService.editCard(bankCard);

        return SUCCESS_TIP;
    }

    /**
     * 取消银行卡绑定信息
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String delete() {
        Long userId = ShiroKit.getUserNotNull().getId();
        if (ToolUtil.isEmpty(userId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        httpClientService.deleteCard(userId);
        return PREFIX + "/bankcard.html";
    }


    /**
     * 查看银行卡详情
     *
     * @author fengshuonan
     */
    @RequestMapping("/look")
    @ResponseBody
    public Object look() {
        Long userId = ShiroKit.getUserNotNull().getId();
        if (ToolUtil.isEmpty(userId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        BankCard bankCard = httpClientService.lookCard(userId);
        return ResponseData.success(bankCard);
    }

    /**
     * 查看银行卡详情
     *
     * @author fengshuonan
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list() {
        Long userId = ShiroKit.getUserNotNull().getId();
        if (ToolUtil.isEmpty(userId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        List<Map<String, Object>> bankcard = httpClientService.list(userId);
        if (bankcard.size() > 0) {
            List<Map<String, Object>> maps = new ArrayList<>();
            maps.add(bankcard.get(0));
            return new BankcardWarpper(maps).wrap();
        }
        return null;
    }

}
